﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArraysAndStrings.LongestPalindrome
{
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(LongestPalindrome("aaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzzyyyyyyyyyyxxxxxxxxxxwwwwwwwwwwvvvvvvvvvvuuuuuuuuuuttttttttttssssssssssrrrrrrrrrrqqqqqqqqqqppppppppppoooooooooonnnnnnnnnnmmmmmmmmmmllllllllllkkkkkkkkkkjjjjjjjjjjiiiiiiiiiihhhhhhhhhhggggggggggffffffffffeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzzyyyyyyyyyyxxxxxxxxxxwwwwwwwwwwvvvvvvvvvvuuuuuuuuuuttttttttttssssssssssrrrrrrrrrrqqqqqqqqqqppppppppppoooooooooonnnnnnnnnnmmmmmmmmmmllllllllllkkkkkkkkkkjjjjjjjjjjiiiiiiiiiihhhhhhhhhhggggggggggffffffffffeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaa"));
            Console.ReadKey();
        }
        public static string LongestPalindrome(string s)
        {
            return GetString(s,true).Length> GetString(s,false).Length ? GetString(s, true):GetString(s, false);
        }

        private static string GetString(string s,bool left)
        {
            if (s.Length == 1) return s[0].ToString();
            if (s.Length == 2 && s[0] == s[1]) return s;
            if (s.Length == 2 && s[0] != s[1]) return s[0].ToString();
            int leftIndex = 0;
            int rightIndex = 0;

            int returnLeftIndex = 0;
            int returnRightIndex = 0;

            for (int i = 0; i < s.Length; i++)
            {

                leftIndex = rightIndex = i;
                if (left)
                {
                    if (i + 1 < s.Length && s[i] == s[i + 1])
                    {
                        leftIndex = i;
                        rightIndex = i + 1;
                    }
                    else if (i - 1 >= 0 && i + 1 < s.Length && s[i] == s[i + 1] && s[i] == s[i - 1])
                    {
                        leftIndex = rightIndex = i;
                    }
                }
                else
                {
                    if (i - 1 >= 0 && i + 1 < s.Length && s[i] == s[i + 1] && s[i] == s[i - 1])
                    {
                        leftIndex = rightIndex = i;
                    }
                    else if (i + 1 < s.Length && s[i] == s[i + 1])
                    {
                        leftIndex = i;
                        rightIndex = i + 1;
                    }

                }



                while (s[leftIndex] == s[rightIndex])
                {
                    leftIndex--;
                    rightIndex++;
                    if (leftIndex < 0 || rightIndex >= s.Length)
                    {
                        rightIndex--;
                        leftIndex++;
                        break;
                    }
                }
                if (s[leftIndex] != s[rightIndex])
                {
                    rightIndex--;
                    leftIndex++;
                }
                if (rightIndex - leftIndex >= returnRightIndex - returnLeftIndex)
                {
                    returnLeftIndex = leftIndex;
                    returnRightIndex = rightIndex;
                }
            }
            return s.Substring(returnLeftIndex/*+1*/, returnRightIndex - returnLeftIndex + 1);
        }
    }
}
